package com.akami.util;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Vector;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import xframe.ctl.XDispatchAction;
import xframe.util.UtilMaster;
import xframe.util.db.Row;
import xframe.util.db.Transaction;

public class SettingAction extends XDispatchAction {

	
	public ActionForward hangleTest( 	ActionMapping       mapping,
			ActionForm          form,
			HttpServletRequest  request,
			HttpServletResponse response ) throws Exception {
			Row	req			=	getRequestInfo(request);
			System.out.println("==============================");
			System.out.println(req);
			request.setAttribute("hangle",charSet(req.getString("hangle")));
			System.out.println("==============================");
			
	return mapping.findForward("hangle");
	}
	
    public static Vector charSet(String str_kr) throws UnsupportedEncodingException{
    	Vector charsetV = new Vector();
        String charset[] = {"euc-kr", "ksc5601", "iso-8859-1", "8859_1", "ascii", "UTF-8"};
            
        for(int i=0; i<charset.length ; i++){
            for(int j=0 ; j<charset.length ; j++){
                if(i==j) continue;
                System.out.println(str_kr + "<=원본 SettingAction :"+charset[i]+" => "+charset[j]+" :"+new String(str_kr.getBytes(charset[i]),charset[j]));
                charsetV.add(str_kr+"<=원본:"+charset[i]+" : "+charset[j]+" =>"+new String(str_kr.getBytes(charset[i]),charset[j]));
                
            }
        }
        return charsetV;
    }
    
    public static Vector charSet(String org, String str_kr) throws UnsupportedEncodingException{
    	Vector charsetV = new Vector();
    	String charset[] = {"euc-kr", "ksc5601", "iso-8859-1", "8859_1", "ascii", "UTF-8"};
    	
    	for(int i=0; i<charset.length ; i++){
    		for(int j=0 ; j<charset.length ; j++){
    			if(i==j) continue;
    			System.out.println(org + "<=원본 SettingAction :"+charset[i]+" => "+charset[j]+" :"+new String(str_kr.getBytes(charset[i]),charset[j]));
    			charsetV.add(org+"<=원본:"+charset[i]+" : "+charset[j]+" :"+new String(str_kr.getBytes(charset[i]),charset[j]));
    			
    		}
    	}
    	return charsetV;
    }
    
	public ActionForward dbTest( 	ActionMapping       mapping,
			ActionForm          form,
			HttpServletRequest  request,
			HttpServletResponse response ) throws Exception {
		Row	req			=	getRequestInfo(request);
		try{
			System.out.println("dbTest==============================");
			
			// 디비연결 시작
			Transaction.start(request);		
			
			//String	listQuery 		=	"select '한글'	 as aa";
			//String	listQuery 		=	"select comment_idx a, reg_name b, reg_date c from comment";
			String	listQuery 		=	"select  a,  b,  c from test";
			simpleExecuteQuery(request, listQuery, "selectxml");	
			
			
			//디비연결 종
			Transaction.end(request);
			
		}catch(Exception e){
			e.printStackTrace();
			Transaction.rollback(request, response);
			req.put("system_error", e.getMessage());
			return mapping.findForward( "system_error" );			
		}    		
		return mapping.findForward("dbTest");
	}
	
	
	public ActionForward insert_selectTest( 	ActionMapping       mapping,
			ActionForm          form,
			HttpServletRequest  request,
			HttpServletResponse response ) throws Exception {
		Row	req			=	getRequestInfo(request);
		System.out.println(UtilMaster.null2Boolean(req.getString("delete"), false));
		
		try{
			// 디비연결 시작
			Transaction.start(request);		
			System.out.println("==============================");
			System.out.println(req);
			request.setAttribute("hangle",charSet(req.getString("hangle")));
			System.out.println("==============================");
			
			String 	listQueryName	=	req.getString("get");
//			String	listQuery 		=	"select '한글'	 as aa";
			long pk = System.currentTimeMillis();
			String	listQuery 		=	"insert into test(a, b, c) values('"+pk+"','"+req.getString("hangle")+"',now())";
			System.out.println("listQuery : "+listQuery);
			simpleExecuteUpdate(request, listQuery, "listQueryName");
			
			listQuery 		=	"select b from test where a='"+pk+"'";
			Row row	=	simpleExecuteQueryGetOneRow(request, listQuery, "selectxml");	
			
			request.setAttribute("hangle",charSet(req.getString("hangle"), row.getString("b")));
			boolean del	=	UtilMaster.null2Boolean(req.getString("delete"), false);
			if(del){
				listQuery 		=	"delete from test where a='"+pk+"'";
				simpleExecuteUpdate(request, listQuery, "listQueryName");
			}
			
			//디비연결 종
			Transaction.end(request);
			
		}catch(Exception e){
			e.printStackTrace();
			Transaction.rollback(request, response);
			req.put("system_error", e.getMessage());
			return mapping.findForward( "system_error" );			
		}    		
		return mapping.findForward("hangle");
	}
	
	public ActionForward testDBcreate( 	ActionMapping       mapping,
			ActionForm          form,
			HttpServletRequest  request,
			HttpServletResponse response ) throws Exception {
		Row	req			=	getRequestInfo(request);
		try{
			// 디비연결 시작
			Transaction.start(request);		
			
			String 	listQueryName	=	req.getString("get");
			String	listQuery 		=	"CREATE TABLE test ( a varchar(30), b varchar(10), c datetime, PRIMARY KEY  (a) )";
			simpleExecuteUpdate(request, listQuery, "selectxml");
			long pk = System.currentTimeMillis();
			listQuery 		=	"insert into test(a, b, c) values('"+pk+"','한글', now())";
			simpleExecuteUpdate(request, listQuery, "listQueryName");
			
			//디비연결 종
			Transaction.end(request);
			req.put("SUCCESS_FAIL", "SUCCESS");
			req.put("MESSAGE", "수정되었습니다.");
		}catch(Exception e){
			req.put("SUCCESS_FAIL", "FAIL");
			req.put("MESSAGE", "실패");
			e.printStackTrace();
			Transaction.rollback(request, response);
			req.put("system_error", e.getMessage());
			return mapping.findForward( "system_error" );			
		}    		

		
		return mapping.findForward( "success_fail" ); 

	}

}
